﻿using HuaweiCloud.Dotnet.Samples.ESports.Common.Messages;
using HuaweiCloud.Dotnet.Samples.ESports.Device.Dto;
using HuaweiCloud.Dotnet.Samples.ESports.Device.Service;
using MassTransit;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HuaweiCloud.Dotnet.Samples.ESports.Device.Controllers
{
    [Route("api-device/v1/[controller]")]
    [Route("api/v1/[controller]")]
    [ApiController]
    public class StepController : ControllerBase
    {
        private readonly IStepService _service;
        private readonly IPublishEndpoint _publishEndpoint;
        public StepController(IStepService service, IPublishEndpoint publishEndpoint)
        {
            _service = service;
            _publishEndpoint = publishEndpoint;
        }
        [HttpGet]
        public async Task<ActionResult<IEnumerable<StepDto>>> Get()
        {
            return Ok(await _service.ListAsync());
        }
        [HttpGet("{id}")]
        public async Task<ActionResult<StepDto>> Get(string id)
        {
            var item = await _service.GetAsync(id);
            return item == null ? NotFound() : Ok(item);
        }
        [HttpPost]
        public async Task<ActionResult> Post([FromBody] CreateStepDto dto)
        {
            await _service.AddAsync(dto);
            await _publishEndpoint.Publish<UpdateStepInfo>(new
            {
                DeviceId = dto.DeviceId,
                Count = dto.Count,
                Date = dto.Date.ToString("yyyy-MM-dd")
            });
            return Accepted();
        }
    }
}
